package ImportDataFromPhone;

import java.io.File;
import java.util.List;
import org.apache.log4j.*;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * 1. Open input file
 *      - since file is small - read all data into the object and close file.
 * 2. Go through each record creating output records
 *      - each output record should contain item name, value, date and category
 *          - pattern for picking item name and value is >(.*) (\d+)<
 *          - date is picked up when encountered and inserted into recurds until
 *            next date is found
 *          - category is assigned if certain pattern is found in item name.
 * 3. Write output file
 *      - write csv file using following format for each line
 *          ^(iten name),(value),(date),(category)$
 */

/**
 *
 * @author ash
 */
public class ImportDataFromPhone {

    private static final Logger log = Logger.getLogger(ImportDataFromPhone.class);
    
    public static void main(String[] argv){

        if (argv.length == 0){
            System.out.println("Input file name missing. Please, provide file name.");
            return;
        }
        
        String inputFile = argv[0];
        File f = new File(inputFile);
        
        if (!f.exists()){
            System.out.println("File "+inputFile+" doesn't exist.");
            return;
        }
        
        if (f.isDirectory()){
            System.out.println(inputFile+" is a directory. Provide name of the text file.");
            return;
        }
     
        //reading raw records from file.
        Container<RawRecord> lst = InputFileReader.getRecords(inputFile);
        log.info("Input file processed.");
        
        //Processing raw records
        Container<ProcessedRecord> records = RawRecordParser.process(lst);
        log.info("Raw records parsed.");
        
        //Adding category to the records
        Container<CategorizedRecord> catRecords = Categorizer.categorize(records);
        log.info("Records categorized.");
        
        //Write processed records to the output file.
        OutputWriter.write(catRecords, FileNameConstructor.appendCSV(inputFile));
        log.info("Output written");

    }
    
   
}
